// pages/media-pixel/index.js
let originUploadList = [
  {
    duration: 330.112,
    fileType: "video",
    height: 272,
    size: 19432020,
    tempFilePath: "http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400",
    thumbTempFilePath: "http://tmp/bkt0SlZb5GdT2355bd63e1bb65402a3ad120eb0586d8.jpg",
    width: 480,
    progress: '100'
  },
  {
    duration: 13.4092,
    fileType: "image",
    height: 1280,
    size: 20364820,
    tempFilePath: "https://th.bing.com/th/id/R.57384e4c2dd256a755578f00845e60af?rik=uy9%2bvT4%2b7Rur%2fA&riu=http%3a%2f%2fimg06file.tooopen.com%2fimages%2f20171224%2ftooopen_sy_231021357463.jpg&ehk=whpCWn%2byPBvtGi1%2boY1sEBq%2frEUaP6w2N5bnBQsLWdo%3d&risl=&pid=ImgRaw&r=0",
    thumbTempFilePath: "http://tmp/9eL1DpcavoSmff4881f09f661c0e3d6f555d8518116a.jpg",
    width: 720,
    progress: '80'
  },
  {
    duration: 330.112,
    fileType: "video",
    height: 272,
    size: 19432020,
    tempFilePath: "http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400",
    thumbTempFilePath: "http://tmp/bkt0SlZb5GdT2355bd63e1bb65402a3ad120eb0586d8.jpg",
    width: 480,
    progress: '0'
  },
  {
    duration: 13.4092,
    fileType: "image",
    height: 1280,
    size: 20364820,
    tempFilePath: "https://th.bing.com/th/id/R.57384e4c2dd256a755578f00845e60af?rik=uy9%2bvT4%2b7Rur%2fA&riu=http%3a%2f%2fimg06file.tooopen.com%2fimages%2f20171224%2ftooopen_sy_231021357463.jpg&ehk=whpCWn%2byPBvtGi1%2boY1sEBq%2frEUaP6w2N5bnBQsLWdo%3d&risl=&pid=ImgRaw&r=0",
    thumbTempFilePath: "http://tmp/9eL1DpcavoSmff4881f09f661c0e3d6f555d8518116a.jpg",
    width: 720,
    progress: '0'
  },
]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 调用函数更新分辨率
    this.updateMediaResolutions(originUploadList).then(() => {
      console.log('媒体分辨率已更新:', originUploadList);
    });
  },

  // 异步函数，处理数组中的每个项目
  async updateMediaResolutions(data) {
    for (let i = 0; i < data.length; i++) {
      let item = data[i];
      if (item.fileType === 'image') {
        try {
          let updatedItem = await this.getImageResolution(item);
          console.log(updatedItem);
          data[i] = updatedItem; // 更新数组中的图片项
        } catch (error) {
          console.error('获取图片分辨率失败:', error);
        }
      }else if (item.fileType === 'video') {
        // 视频分辨率获取可能需要服务器端支持或第三方API
        // 这里假设我们有一个函数getVideoResolution，它返回一个Promise
        // 并解析视频URL以获取分辨率
        try {
          let updatedItem = await this.getVideoResolution(item); 
          data[i] = updatedItem; // 更新数组中的视频项
        } catch (error) {
          console.error('获取视频分辨率失败:', error);
        }
      }
    }
  },

  // 异步函数，获取图片分辨率
  async getImageResolution(imageItem) {
    return new Promise((resolve, reject) => {
      wx.getImageInfo({
        src: imageItem.tempFilePath, // 图片路径
        success: (res) => {
          console.log(res);
          // res为图片信息，包含width和height
          resolve({ ...imageItem, ...res });
          // resolve({ ...imageItem, width: res.width, height: res.height });
        },
        fail: (err) => {
          reject(err);
        }
      });
    });
  },

  // 异步函数，获取视频信息
  async getVideoResolution(videoItem) {
    return new Promise((resolve, reject) => {
      wx.getVideoInfo({
        src: videoItem.tempFilePath, // 图片路径
        success: (res) => {
          // res为图片信息，包含width和height
          resolve({ ...videoItem, ...res });
          // resolve({ ...videoItem, width: res.width, height: res.height });
        },
        fail: (err) => {
          reject(err);
        }
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})